package org.example.admin.controller;

import java.util.List;

import org.example.admin.bo.MenuBO;
import org.example.admin.dto.MenuDto;
import org.example.admin.feign.Hfeign;
import org.example.admin.vo.MenuVo;
import org.example.core.constant.SecurityConstants;
import org.example.core.utils.PageUtils;
import org.example.core.utils.R;
import org.example.core.valid.group.AddGroup;
import org.example.core.valid.group.UpdateGroup;
import org.example.security.annotation.RequirePermission;
import org.springframework.beans.factory.annotation.Autowired;

import org.example.admin.entity.MenuEntity;
import org.example.admin.service.MenuService;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;


/**
 * @author 小杰_cc
 * @version 1.0
 */
@RestController
@RequestMapping("/sys/backend/menu")
public class MenuController {
    @Autowired
    private MenuService menuService;

    @Autowired
    private Hfeign hfeign;

    /**
     * 测试接口
     * @return
     */
    @GetMapping("/w")
    public String w(){
        R<?> h = hfeign.h(SecurityConstants.INTERNAL);
        if (h.getCode()==0){
            return "success";
        }
        return "error";
    }

    /**
     * 权限树数据
     */
    @GetMapping("/tree")
    public R<?> tree(){
        List<MenuVo> menuVos = menuService.tree();
        return R.ok(menuVos);
    }
    /**
     * 菜单列表
     */
    @RequirePermission("sys:menu:list")
    @GetMapping("/list")
    public R<?> list(MenuDto menuDto){
        PageUtils pageUtils = menuService.list(menuDto);
        return R.ok(pageUtils);
    }
    /**
     * 单个菜单信息
     */
    @GetMapping("/{id}/detail")
    public R<?> info(@PathVariable("id") Long id){
        return R.ok(menuService.detail(id));
    }
    /**
     * 保存菜单信息
     */
    @PostMapping("/save")
    public R<?> save(@Validated(value = {AddGroup.class}) @RequestBody MenuBO menu){
        return menuService.save(menu)>0? R.ok():R.error();
    }

    /**
     * 修改菜单信息
     */
    @PutMapping("/update")
    public R<?> update(@Validated(value = {UpdateGroup.class}) @RequestBody MenuBO menu){
        return menuService.update(menu)>0? R.ok():R.error();
    }

    /**
     * 删除
     */
    @DeleteMapping("/delete")
    public R<?> delete(@RequestBody List<Long> ids){
        return menuService.delete(ids);
    }

    // 获取前端路由菜单
    @GetMapping("/routers")
    public R<?> routerList(){
        List<MenuVo> menuVos = menuService.routerList();
        return R.ok(menuService.buildRouterMenu(menuVos));
    }
}